package com.sympsel.Optimal_Algorithm_OA;

import org.junit.Test;

import java.util.Arrays;

public class BitOperation {
    @Test
    public void test1() {
        int a = 5;
        System.out.println(a + ": " + Integer.toBinaryString(a));
        System.out.println(~a + ": " + Integer.toBinaryString(~a));

        int b = 5;
        System.out.println(b + ": " + Integer.toBinaryString(b));
        System.out.println((b >>> 1) + ": " + Integer.toBinaryString(b + 1));
        System.out.println((b >> 1) + ": " + Integer.toBinaryString(b + 1));

        int c = -5;

        System.out.println(c + ": " + Integer.toBinaryString(c));
        System.out.println((c >>> 1) + ": " + Integer.toBinaryString(c + 1));
        System.out.println((c >> 1) + ": " + Integer.toBinaryString(c + 1));

        int d = -5;
        System.out.println(d + ": " + Integer.toBinaryString(d));
        System.out.println((~d + 1) + ": " + Integer.toBinaryString(~d + 1));

        System.out.println(Integer.toBinaryString(5 &~ (1 << 1)));
        System.out.println(5 &~ (1 << 1));
    }

    public boolean isUnique(String astr) {
        int len = astr.length();
        if (len > 26) {
            return false;
        }
        for (int i = 0, bit = 0; i < len; i++) {
            int k = astr.charAt(i) - 'a';
            if (((bit >> k) & 1) == 1) {
                return false;
            }
            bit |= (1 << k);
        }
        return true;
    }

    @Test
    public void test2() {
        System.out.println(isUnique("abc"));
        System.out.println(isUnique("abca"));
        System.out.println(isUnique("leetcode"));
    }

    public int getSum(int a, int b) {
        // 加法器 2025/10/5
        int S = a ^ b, C = ((a & b) << 1);
        if (C == 0) {
            return S;
        }
        return getSum(S, C);
    }

    @Test
    public void test3() {
        System.out.println(getSum(1, 2));
        System.out.println(getSum(2, 3));
        System.out.println(getSum(5, 7));
    }

    public int singleNumber(int[] nums) {
        int ret = 0;
        for (int i = 0;i < 32; i++) {
            int sum = 0;
            for (int x : nums) {
                sum += (x >> i) & 1;
            }
            sum %= 3;
            if (sum == 1) {
                ret |= (1 << i);
            } else {
                ret &= ~(1 << i);
            }
        }
        return ret;
    }

    @Test
    public void test4() {
        System.out.println(singleNumber(new int[]{1, 2, 1, 2, 3, 2, 1}));
    }

    public int[] missingTwo(int[] nums) {
        int eor = 0;
        for (int num : nums) {
            eor ^= num;
        }
        for (int i = 1; i <= nums.length + 2; i++) {
            eor ^= i;
        }
        int rightOne = eor & (~eor + 1);
        int[] ret = new int[2];
        for (int num : nums) {
            if ((num & rightOne) == 0) {
                ret[0] ^= num;
            }
        }

        for (int i = 1; i <= nums.length + 2; i++) {
            if ((i & rightOne) == 0) {
                ret[0] ^= i;
            }
        }
        ret[1] = eor ^ ret[0];
        return ret;
    }

    @Test
    public void test5() {
        System.out.println(Arrays.toString(missingTwo(new int[]{1, 2, 4, 5, 7, 8, 9})));
    }
}
